package btd.game;

import java.util.ArrayList;

import btd.game.rule.Board;

@SuppressWarnings("serial")
public class Steps extends ArrayList<String> {

	public Steps() {
		super();
	}

	public boolean add(Board board, String color, int row,
			int col, boolean isPass, int dCount) {
		return super.add(board.toString() + color + "\n" + String.valueOf(row)
				+ "\n" + String.valueOf(col) + "\n" + String.valueOf(isPass)
				+ "\n" + String.valueOf(dCount));
	}

	@Override
	public Steps clone() {
		super.clone();
		Steps s = new Steps();
		for (String str : this) {
			s.add(str);
		}
		return s;
	}

	public byte[][] getBBoard(int index) {
		byte[][] board = new byte[19][19];
		if (this.size() > 0) {
			String[] rows = this.get(index).split("\n");
			if (ConstData.ISHEIQI) {
				for (int i = 0; i < 19; i++) {
					String row = rows[i];
					String[] cols = row.split(" ");
					for (int j = 1; j < 20; j++) {
						if ("b".equals(cols[j])) {
							board[i][j - 1] = ConstData.QIZI_BLACK;
						}
						if ("w".equals(cols[j])) {
							board[i][j - 1] = ConstData.QIZI_WHITE;
						}
						if (".".equals(cols[j])) {
							board[i][j - 1] = 0;
						}
					}
				}
			} else {
				for (int i = 0; i < 19; i++) {
					String row = rows[i];
					String[] cols = row.split(" ");
					for (int j = 1; j < 20; j++) {
						if ("b".equals(cols[j])) {
							board[18 - i][19 - j] = ConstData.QIZI_BLACK;
						}
						if ("w".equals(cols[j])) {
							board[18 - i][19 - j] = ConstData.QIZI_WHITE;
						}
						if (".".equals(cols[j])) {
							board[18 - i][19 - j] = 0;
						}
					}
				}
			}
		}
		return board;
	}

	public byte[][] getWBoard(int index) {
		byte[][] board = new byte[19][19];
		if (this.size() > 0) {
			String[] rows = this.get(index).split("\n");
			if (ConstData.ISHEIQI) {
				for (int i = 0; i < 19; i++) {
					String row = rows[i];
					String[] cols = row.split(" ");
					for (int j = 1; j < 20; j++) {
						if ("b".equals(cols[j])) {
							board[18 - i][19 - j] = ConstData.QIZI_BLACK;
						}
						if ("w".equals(cols[j])) {
							board[18 - i][19 - j] = ConstData.QIZI_WHITE;
						}
						if (".".equals(cols[j])) {
							board[18 - i][19 - j] = 0;
						}
					}
				}
			} else {
				for (int i = 0; i < 19; i++) {
					String row = rows[i];
					String[] cols = row.split(" ");
					for (int j = 1; j < 20; j++) {
						if ("b".equals(cols[j])) {
							board[i][j - 1] = ConstData.QIZI_BLACK;
						}
						if ("w".equals(cols[j])) {
							board[i][j - 1] = ConstData.QIZI_WHITE;
						}
						if (".".equals(cols[j])) {
							board[i][j - 1] = 0;
						}
					}
				}
			}
		}
		return board;
	}

	public boolean isPass(int index) {
		String[] rows = this.get(index).split("\n");
		if (rows[22].equals("true")) {
			return true;
		}
		return false;
	}

	public String getColor(int index) {
		return this.get(index).split("\n")[19];
	}

	public int getRow(int index) {
		return Integer.parseInt(this.get(index).split("\n")[20]);
	}

	public int getCol(int index) {
		return Integer.parseInt(this.get(index).split("\n")[21]);
	}

	public int getDCount(int index) {
		return Integer.parseInt(this.get(index).split("\n")[23]);
	}
}